#include<iostream>
#include<string.h>
#include<fstream>
using namespace std;
typedef struct good 
{
 string type;//商品类型
 string name;//商品名称;
 string factor;//生产厂家;
 string  pinpai;//商品品牌 
 double price;//价格；
 int amount;//数量；
 int sum;	//总和 
 int  m;//表示有几个商品种类 
 struct good *next;
 struct good *head=NULL;
}*goodptr;
	goodptr head=NULL;
class goodlistlink
{
public:
	void xuanze();
	void shuru();
	void menu();
	void chucun();
	void prechucun(); 
};
void goodlistlink::shuru()
{
	goodptr p1,p2;
	if(head==NULL)
	{
	head=new good;
	head->next=NULL;
	}
	int  m;
	cout<<"请输入有几个商品种类"<<endl;
	cin>>m;
	fseek(stdin,0,SEEK_END);
	if(m<0)
	{
		cout<<"错误,总数应不小于0"<<endl;
		return ; 
	}
	p2=head=new good;
	p2->type='\0';
	p2->name='\0';
	p2->factor='\0';
	p2->pinpai='\0';
	p2->price=0;
	p2->amount=0;
	head->next=p1;		//表为空时，表头即为表尾
	for(int i=0;i<m;i++)
	{
		p1=new good;		//为新节点开辟内存空间
		cout<<"请输入商品类型 名称 厂家 品牌 价格  数量 "<<endl;
		cin>>p1->type>>p1->name>>p1->factor>>p1->pinpai>>p1->price>>p1->amount;
		 p2->next=p1;
		 p2=p1;
	}
	p1->next=NULL;
	 cout<<"商品链表创建成功"<<endl; 
 } 
void goodlistlink::menu()
 {
 	cout<<"\n\t\t\t\t\t    1.输入商品信息"; 
	cout<<"\n\t\t\t\t\t    2.储存商品信息"; 
	cout<<"\n\t\t\t\t\t    3.输出上次储存的信息"; 
	cout<<"\n\t\t\t\t\t    0.退出系统                   ";
	cout<<"\n\t\t\t\t\t    请输入相关选项               "; 
	cout<<"\n"; 
 }
 //功能选择
 void goodlistlink::xuanze()
 {
    int  i;
    cin>>i;
    if(i>=0&&i<=9)
    {
    	switch(i)
    	{
    		goodlistlink  B; 
    		case 1:B.shuru();break;
			case 2:B.chucun();break; 
			case 3:B.prechucun();break; 
			case 0: printf("谢谢使用！");exit(0);
			default: printf("输入有误！");break;
		}
	}
	else
    {
       printf("输入有误!请输入数字！\n");	
	} 
	getchar();
	getchar();
	return ; 
} 
 //储存信息
void goodlistlink::chucun()
{
	
	goodptr p=NULL;
	p=new good();
	p=p->next;
	ofstream outfile("text.txt",ios::out);
	if(!outfile)
	{
		cout<<"储存失败"<<endl; 
		abort(); 
	}
	else
	{
		while(p!=NULL)
		{
			 
			outfile.write((char*)&p,sizeof(goodlistlink));
			p=p->next; 
		} 
		cout<<"储存成功"<<endl; 
		outfile.close(); 
	} 
 } 
 
 //输出上次储存的信息
void goodlistlink::prechucun()
{
	goodptr p;
	p=head->next; 
	 ifstream infile("text.txt",ios::in);
	 if(!infile)
	 {
	 	cout<<"显示失败"<<endl; 
	 	abort(); 
	 } 
	 else
	 {
	 	while(p)
	 	 {
	 		infile.read((char*)&p,sizeof(good));
	 		p=p->next;
	 		cout<<"商品类型 名称 厂家 品牌 价格  数量"<<endl; 
			cout<<p->type<<p->name<<p->factor<<p->pinpai<<p->price<<p->amount;
		 } 
		 
		 cout<<"显示成功"<<endl; 
		 infile.close(); 
	 } 
} 
int main()
{
	goodlistlink  A;
	while(1)
	{
		A.menu();
		A.xuanze();
	}
	return 0; 
}